package com.example.sports_meet.subject;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.sports_meet.MyDbHelper;
import com.example.sports_meet.R;

import java.util.Objects;

public class subject_add extends AppCompatActivity {
    private EditText editText1;
    private EditText editText2;
    private Button button;
    private Button button_add;
    private MyDbHelper myDbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subject_add);
        Objects.requireNonNull(getSupportActionBar()).hide();

        editText1 = findViewById(R.id.editText1);
        editText2 = findViewById(R.id.editText2);

        myDbHelper = new MyDbHelper(this, "mydb", null, 1);
        SQLiteDatabase db = myDbHelper.getWritableDatabase();
//        db.delete("form", null, null);
//        myDbHelper.insertFormData(db);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        button_add = findViewById(R.id.button_add);
        button_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String id = editText1.getText().toString();
                String name = editText2.getText().toString();
                if (id.length() == 0 || name.length() == 0) {
                    Toast.makeText(subject_add.this, "请输入内容", Toast.LENGTH_SHORT).show();
                } else if (!id.matches("\\d+")) {
                    Toast.makeText(subject_add.this, "编号只能为数字", Toast.LENGTH_SHORT).show();
                } else {
                    SQLiteDatabase db = myDbHelper.getWritableDatabase();
                    ContentValues contentValues = new ContentValues();
                    contentValues.put("fno", id);
                    contentValues.put("fname", name);
                    if (db.insert("form", null, contentValues) >= 1) {
                        Toast.makeText(subject_add.this, "添加成功", Toast.LENGTH_SHORT).show();
                    }
                }

            }
        });


    }
}